package br.com.mocamorena.view.panel;

import br.com.mocamorena.model.Cliente;
import br.com.mocamorena.view.UtilitiesGUI;
import javax.swing.JLabel;

public final class PessoaJuridicaPanel extends javax.swing.JPanel {

    public PessoaJuridicaPanel() {
        initComponents();
        hideErrorsLabels();
    }

    public void hideErrorsLabels() {
        razaoSocialErrorLabel.setVisible(false);
        nomeFantasiaErrorLabel.setVisible(false);
        cnpjErrorLabel.setVisible(false);
        representanteErrorLabel.setVisible(false);
    }

    public String getRazaoSocial() {
        return razaoSocialField.getText().trim().toUpperCase();
    }

    private void setRazaoSocial(String razaoSocial) {
        razaoSocialField.setText(razaoSocial);
    }

    private String getNomeFantasia() {
        return nomeFantasiaField.getText().trim().toUpperCase();
    }

    private void setNomeFantasia(String nomeFantasia) {
        nomeFantasiaField.setText(nomeFantasia);
    }

    private String getCnpj() {
        if(".   .   /    -".equals(cnpjField.getText().trim())) {
            return null;
        }
        return cnpjField.getText().trim();
    }

    private void setCnpj(String cnpj) {
        cnpjField.setText(cnpj);
    }

    private String getRepresentante() {
        return representanteField.getText().trim().toUpperCase();
    }

    private void setRepresentante(String representante) {
        representanteField.setText(representante);
    }

    public JLabel getRazaoSocialErrorLabel() {
        return razaoSocialErrorLabel;
    }

    public void fillEntity(Cliente pessoaJuridica) {
        if (pessoaJuridica == null) {
            return;
        }

        pessoaJuridica.setNome(getRazaoSocial());
        pessoaJuridica.setEmpresa(getNomeFantasia());
        pessoaJuridica.setCadastroPessoa(getCnpj());
        pessoaJuridica.setRepresentante(getRepresentante());
    }

    public void fillPanel(Cliente pessoaJuridica) {
        if (pessoaJuridica == null) {
            return;
        }

        setRazaoSocial(pessoaJuridica.getNome());
        setNomeFantasia(pessoaJuridica.getEmpresa());
        setCnpj(pessoaJuridica.getCadastroPessoa());
        setRepresentante(pessoaJuridica.getRepresentante());
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        razaoSocialLabel = new javax.swing.JLabel();
        razaoSocialField = new javax.swing.JTextField();
        cnpjLabel = new javax.swing.JLabel();
        cnpjField = new javax.swing.JFormattedTextField();
        representanteLabel = new javax.swing.JLabel();
        representanteField = new javax.swing.JTextField();
        nomeFantasiaField = new javax.swing.JTextField();
        nomeFantasiaLabel = new javax.swing.JLabel();
        razaoSocialErrorLabel = new javax.swing.JLabel();
        nomeFantasiaErrorLabel = new javax.swing.JLabel();
        cnpjErrorLabel = new javax.swing.JLabel();
        representanteErrorLabel = new javax.swing.JLabel();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        razaoSocialLabel.setText("Razão Social:");
        add(razaoSocialLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(17, 0, 65, 20));
        add(razaoSocialField, new org.netbeans.lib.awtextra.AbsoluteConstraints(99, 0, 383, -1));
        UtilitiesGUI.respondToArrowEnterKey(razaoSocialField);

        cnpjLabel.setText("CNPJ:");
        add(cnpjLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(52, 52, 47, 20));

        try {
            cnpjField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        cnpjField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);
        add(cnpjField, new org.netbeans.lib.awtextra.AbsoluteConstraints(99, 52, 130, -1));
        UtilitiesGUI.respondToArrowEnterKey(cnpjField);

        representanteLabel.setText("Representante:");
        add(representanteLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(5, 78, 94, 20));
        add(representanteField, new org.netbeans.lib.awtextra.AbsoluteConstraints(99, 78, 383, -1));
        UtilitiesGUI.respondToArrowEnterKey(representanteField);
        add(nomeFantasiaField, new org.netbeans.lib.awtextra.AbsoluteConstraints(99, 26, 383, -1));
        UtilitiesGUI.respondToArrowEnterKey(nomeFantasiaField);

        nomeFantasiaLabel.setText("Nome Fantasia:");
        add(nomeFantasiaLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(6, 26, 76, 20));

        razaoSocialErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/exclamation.png"))); // NOI18N
        add(razaoSocialErrorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(486, 0, 20, 20));

        nomeFantasiaErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/exclamation.png"))); // NOI18N
        add(nomeFantasiaErrorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(486, 26, 20, 20));

        cnpjErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/exclamation.png"))); // NOI18N
        add(cnpjErrorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(234, 52, 20, 20));

        representanteErrorLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/exclamation.png"))); // NOI18N
        add(representanteErrorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(486, 80, 20, -1));
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JLabel cnpjErrorLabel;
    private javax.swing.JFormattedTextField cnpjField;
    private javax.swing.JLabel cnpjLabel;
    protected javax.swing.JLabel nomeFantasiaErrorLabel;
    private javax.swing.JTextField nomeFantasiaField;
    private javax.swing.JLabel nomeFantasiaLabel;
    protected javax.swing.JLabel razaoSocialErrorLabel;
    private javax.swing.JTextField razaoSocialField;
    private javax.swing.JLabel razaoSocialLabel;
    protected javax.swing.JLabel representanteErrorLabel;
    private javax.swing.JTextField representanteField;
    private javax.swing.JLabel representanteLabel;
    // End of variables declaration//GEN-END:variables
}
